//
//  MainController.swift
//  vmes
//
//  Created by user on 2021/10/22.
//

import UIKit

class MainController: BaseController,LBXScanViewControllerDelegate {
    
    
    let topLayout=UIView()
    
    override func viewWillAppear(_ animated: Bool) {
        navigationController?.setNavigationBarHidden(true, animated: false)
    }
    
    override func viewDidLoad(){
        super.viewDidLoad()
        view.backgroundColor = UIColor.colorWithHex(hexStr: "#0FA386")
        initPersonView()
        let normalView=UIView()
        normalView.backgroundColor = .white
        view.addSubview(normalView)
        normalView.snp.makeConstraints { make in
            make.top.equalTo(topLayout.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        let scanBtn=UIImageView(image: UIImage(named: "img_scan"))
        let label1=UILabel()
        label1.text="扫一扫"
        let label2=UILabel()
        label2.text="请扫设备二维码，获取工单信息"
        normalView.addSubview(scanBtn)
        normalView.addSubview(label1)
        normalView.addSubview(label2)
        scanBtn.snp.makeConstraints { make in
            make.centerX.equalTo(view.snp.centerX)
            make.top.equalToSuperview().offset(85)
        }
        label1.snp.makeConstraints { make in
            make.centerX.equalTo(view.snp.centerX)
            make.top.equalTo(scanBtn.snp.bottom).offset(10)
        }
        label2.snp.makeConstraints { make in
            make.centerX.equalTo(view.snp.centerX)
            make.top.equalTo(label1.snp.bottom).offset(10)
        }
        //        let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(clickImage))
        scanBtn.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickImage)))
        scanBtn.isUserInteractionEnabled=true
        initBottomView()
        
    }
    
    @objc func clickImage(){
        ZhiFuBaoStyle()
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    func ZhiFuBaoStyle() {
        //设置扫码区域参数
        var style = LBXScanViewStyle()
        style.centerUpOffset = 60
        style.xScanRetangleOffset = 30
        if UIScreen.main.bounds.size.height <= 480 {
            //3.5inch 显示的扫码缩小
            style.centerUpOffset = 40
            style.xScanRetangleOffset = 20
        }
        style.color_NotRecoginitonArea = UIColor(red: 0.4, green: 0.4, blue: 0.4, alpha: 0.4)
        style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle.Inner
        style.photoframeLineW = 2.0
        style.photoframeAngleW = 16
        style.photoframeAngleH = 16
        
        style.isNeedShowRetangle = false
        
        style.anmiationStyle = LBXScanViewAnimationStyle.NetGrid
        style.animationImage = UIImage(named: "CodeScan.bundle/qrcode_scan_full_net")
        
        let vc = LBXScanViewController()
        vc.title="扫码"
        navigationController?.navigationBar.tintColor = .white
        vc.scanStyle = style
        vc.isSupportContinuous = false;
        vc.scanResultDelegate = self
        self.navigationController?.pushViewController(vc, animated: false)
        
    }
    
    func initPersonView(){
        //        topLayout.alpha=0.2
        topLayout.backgroundColor = UIColor.colorWithHex(hexStr: "#3FB59E",alpha: 1)
        topLayout.layer.shadowColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.5).cgColor
        topLayout.frame=CGRect(x: 15, y: statusBarHeight, width: width-30, height: 110)
        topLayout.roundCorners([.topLeft,.topRight], radius: 10)
        topLayout.layer.shadowOpacity = 1
        topLayout.layer.shadowRadius = 1.5
        view.addSubview(topLayout)
        
        let nameLb=UILabel()
        nameLb.textColor = .white
        nameLb.font=nameLb.font.withSize(18)
        topLayout.addSubview(nameLb)
        let roleLb=SHLabel()
        roleLb.textInsets=UIEdgeInsets(top: 3, left: 10, bottom: 3, right: 10)
        
        topLayout.addSubview(roleLb)
        let exitBtn=UILabel()
        exitBtn.text="退出"
        topLayout.addSubview(exitBtn)
        let companyLb=UILabel()
        
        companyLb.textColor = .white
        companyLb.font=companyLb.font.withSize(15)
        topLayout.addSubview(companyLb)
        nameLb.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(22)
        }
        companyLb.snp.makeConstraints { make in
            make.top.equalTo(nameLb.snp.bottom).offset(20)
            make.left.equalToSuperview().offset(20)
        }
        roleLb.textColor = .white
        roleLb.layer.cornerRadius=12
        roleLb.font=roleLb.font.withSize(14)
        roleLb.layer.backgroundColor = UIColor.colorWithHex(hexStr: "#ffffff", alpha: 0.2).cgColor
        roleLb.snp.makeConstraints { make in
            make.left.equalTo(nameLb.snp.right).offset(20)
            make.bottom.equalTo(nameLb.snp.bottom)
        }
        exitBtn.textColor = .white
        exitBtn.font=exitBtn.font.withSize(15)
        exitBtn.alpha=0.5
        exitBtn.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-25)
        }
        
        exitBtn.isUserInteractionEnabled=true
        exitBtn.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showDia)))
        
        //设置数据
        let userInfo = K.userInfo
        nameLb.text=userInfo?.nickName
        roleLb.text=userInfo?.post?.postName
        companyLb.text=userInfo?.company?.companyName
        K.currentWorkShop=userInfo?.workshops?.first
        
    }
    
    @objc func showDia(){
        self.showAlert()
    }
    
    func initBottomView(){
        let warnLabel=UILabel()
        let scanLabel=UILabel()
        view.addSubview(warnLabel)
        view.addSubview(scanLabel)
        let attrStr = NSAttributedString(string: "  报警信息", attributes: [.foregroundColor : UIColor.colorWithHex(hexStr: "#FF7B44")])
        // 图文混排
        let attacment = NSTextAttachment()
        attacment.image = UIImage(named: "icon_warn")
        // 获取文字字体
        let font = warnLabel.font
        attacment.bounds = CGRect(x: 0, y: -4, width: (font?.lineHeight)!, height: (font?.lineHeight)!)
        let attrImageStr = NSAttributedString(attachment: attacment)
        let attrMStr = NSMutableAttributedString()
        attrMStr.append(attrImageStr)
        attrMStr.append(attrStr)
        warnLabel.attributedText = attrMStr
        warnLabel.backgroundColor = .white
        warnLabel.textAlignment = .center
        warnLabel.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview()
            make.height.equalTo(60)
            make.width.equalTo(width/2)
        }
        
        // 图文混排
        let attrStr2 = NSAttributedString(string: "  开工列表", attributes: [.foregroundColor : UIColor.colorWithHex(hexStr: "#4594D5")])
        let attacment2 = NSTextAttachment()
        attacment2.image = UIImage(named: "icon_open_list")
        // 获取文字字体
        let font2 = scanLabel.font
        attacment2.bounds = CGRect(x: 0, y: -4, width: (font2?.lineHeight)!, height: (font2?.lineHeight)!)
        let attrImageStr2 = NSAttributedString(attachment: attacment2)
        let attrMStr2 = NSMutableAttributedString()
        attrMStr2.append(attrImageStr2)
        attrMStr2.append(attrStr2)
        scanLabel.attributedText = attrMStr2
        //        scanLabel.text = "切换设备"
        //        scanLabel.textColor=UIColor.colorWithHex(hexStr: "#099A80")
        scanLabel.backgroundColor = .white
        scanLabel.textAlignment = .center
        scanLabel.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(60)
            make.width.equalTo(width/2)
        }
        warnLabel.isUserInteractionEnabled=true
        warnLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(toWarn)))
        scanLabel.isUserInteractionEnabled=true
        scanLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(toOpenList)))
    }
    
    @objc func toWarn(){
        let vc=WarnListController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    @objc func toOpenList(){
        let vc=OpenListController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    func scanFinished(scanResult: LBXScanResult, error: String?) {
        NSLog("scanResult:\(scanResult)")
        let str=scanResult.strScanned!
        let response = try? JSONDecoder().decode(ScanCode.self, from:  str.data(using: .utf8)!)
        DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(400)) {
            if(response?.workshopId == K.currentWorkShop?.workshopId){
                let vc = OrderListController()
                vc.scanResult = response
                self.navigationController?.pushViewController(vc, animated: true)
            }else{
                self.view.makeToast("数据错误")
            }
            
        }
    }
}
